/*
 * Created: 2022-08-31 20:17:50
 * Author : 涛涛涛
 */
import React, { FC, useContext, useEffect, useState } from 'react';
import { ProductExhibitionContext } from '@/pages/ProductExhibition/ProductExhibitionContext';
import { figureTime_hhmmss } from '@/utils';
type DownTime = ReturnType<typeof figureTime_hhmmss>;
export const useDownTime = () => {
    const {
        product: { dropEndDate },
    } = useContext(ProductExhibitionContext);
    const [downTime, setDownTime] = useState<DownTime>(() =>
        figureTime_hhmmss(new Date(dropEndDate).getTime() - Date.now())
    );
    useEffect(() => {
        if (!dropEndDate) return;
        const intervalId = setInterval(() => {
            const _downTime = figureTime_hhmmss(new Date(dropEndDate).getTime() - Date.now());
            setDownTime(_downTime);
        }, 1000);
        return () => {
            clearInterval(intervalId);
        };
    }, [dropEndDate]);
    return {
        downTime,
    };
};
